/*
网站管理员Store，超级客服，拥有所有权限
*/

import { defineStore } from "pinia";
import type { Ref } from "vue";
import { computed, ref } from "vue";

import type {
  IWebsite,
  
  IWebsiteService,
  ICustomer,

  IConversation,
  IConversationRelation,
} from "@src/types";
import { CurrencyDollarIcon } from "@heroicons/vue/24/outline";

const useServiceStore = defineStore("useServiceStore", () => {

  //当前登录的客服
  const currentService: Ref<IWebsiteService | undefined> = ref(undefined)
  const currentConversationRelationId: Ref<number | undefined> = ref(undefined)
  const website: Ref<IWebsite | undefined> = ref(undefined);
  const conversationRelations: Ref<Record<string,IConversationRelation>> = ref({});
  const conversationRelationsHistory: Ref<Record<string,IConversationRelation>> = ref({});
  const showComplete: Ref<boolean> = ref(false);
  const services: Ref<Record<string,IWebsiteService>> = ref({});
  const customers: Ref<Record<string,ICustomer>> = ref({});
  const ws: Ref<WebSocket | undefined> = ref(undefined);
  const loadMoreFlag: Ref<Boolean> = ref(false);
  function logout(){
    currentService.value = undefined;
    currentConversationRelationId.value = undefined;
    website.value = undefined;
    conversationRelations.value = {};
    services.value = {};
    customers.value = {};
    ws.value?.close();
    ws.value = undefined;
  }

  //ai回复，刷新网页丢失
  const aiConversations: Ref<Record<number,string[]>> = ref({});

  function addAiMessage(message:string,relation_id:number) {
      if (!(relation_id in aiConversations.value)) {
        aiConversations.value[relation_id] = []
      }
      aiConversations.value[relation_id].push(message)
  }

  function addConversation(conversation:IConversation) {
    if (conversation.relation_id in conversationRelations.value) {
      const relation = conversationRelations.value[conversation.relation_id];
      relation.conversations[conversation.id] = conversation;
      if (currentConversationRelationId.value && currentConversationRelationId.value != relation.id) {
        relation.hasNew = true;
      }
    } else if (conversation.relation_id in conversationRelationsHistory.value) {
      const relation = conversationRelationsHistory.value[conversation.relation_id];
      if (!relation.conversations) {
        relation.conversations = {};
      }
      relation.conversations[conversation.id] = conversation;
    }
  }

  function customerOffLine(customer_id:number) {
    for (const key in conversationRelations.value) {
      const relation = conversationRelations.value[key];
      if (relation.customer_id == customer_id) {
        relation.customer_status = 'off'
      }
    }
  }
  return {
    loadMoreFlag,
    showComplete,
    logout,
    addAiMessage,
    aiConversations,
    customerOffLine,
    addConversation,
    ws,
    currentConversationRelationId,
    currentService,
    services,
    website,
    customers,
    conversationRelations,
    conversationRelationsHistory
  };
});

export default useServiceStore;
